package juc.atomic;


import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerDetailsTest {
    public static void main(String[] args) {
        //2种方式创建，无参、有参
        AtomicInteger i=new AtomicInteger();
        System.out.println(i.get());
        i=new AtomicInteger(10);
        System.out.println(i.get());

        //set()没有锁，主要用来初始化
        i.set(12);
        System.out.println(i.get());

        i.lazySet(13);

        //compareAndSet()，对比并设置，CAS算法
        i.getAndSet(1);
        //先赋值再加1
        i.getAndIncrement();
        //先赋值再减1
        i.getAndDecrement();
        //先赋值再加delta
        i.getAndAdd(2);
        //转换成int类型
        int i1 = i.intValue();


        AtomicInteger getAndSet2=new AtomicInteger();
        new Thread(){
            @Override
            public void run() {
                for (int j = 0; j < 10; j++) {
                    int v=getAndSet2.addAndGet(1);
                    System.out.println(v);
                }
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
                for (int j = 0; j < 10; j++) {
                    int v = getAndSet2.addAndGet(1);
                    System.out.println(v);
                }
            }
        }.start();
    }
}
